<?php

defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Description of Users
 *
 * @author doufu <442958506@qq.com>
 */
class Agent extends REST_D_Controller {

	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
		$this->load->model('api/Agent_model', 'Agent');
		$this->load->library('form_validation');
	}

	/**
	 * 短信验证
	 */
	public function sms_verify() {
		$this->form_validation->validateData(array(
			'username',
			'code'
				), array(
			array(
				'username',
				'mobile'
			),
			array(
				'code',
				'ingeter'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->library('vendor_interface');

		$result = $this->vendor_interface->verify($this->form_validation->username, $this->form_validation->code);
		if ($result) {
			$this->renderJson(11100);
		} else {
			$this->renderJson(11200);
		}
	}

	/**
	 * 注册
	 */
	public function signup() {
		$this->form_validation->validateData(array(
			'address_id',
			'realname',
			'username',
			'password',
			'code'
				), array(
			array(
				'address_id',
				'required|greater_than[0]'
			),
			array(
				'realname',
				'required|max_length[20]'
			),
			array(
				'username',
				'required|mobile|is_unique[agent.username]'
			),
			array(
				'password',
				'required|min_length[6]'
			),
			array(
				'code',
				'required|integer'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->library('vendor_interface');
		$ret = $this->vendor_interface->verify($this->form_validation->username, $this->form_validation->code);
		if (!$ret) {
			$this->renderJson(10101);
		}

		$this->load->library('session');
		$this->session->sess_regenerate(TRUE);
		$token = session_id();
		$time = time();
		$dataInsert = array(
			'username' => $this->form_validation->username,
			'nickname' => $this->form_validation->realname,
			'password_hash' => md5(md5($this->form_validation->password)),
			'status' => parent::STATUS_DELETE,
			'access_token' => $token,
			'created_at' => $time,
			'updated_at' => $time
		);
		$data2 = array(
			'agent_id' => 0,
			'realname' => $this->form_validation->realname,
			'created_at' => $time,
			'updated_at' => $time,
		);
		$data3 = array(
			'agent_id' => 0,
			'address_id' => $this->form_validation->address_id,
			'status' => parent::STATUS_ACTIVE,
			'created_at' => $time,
			'updated_at' => $time
		);
		$result = $this->Agent->addOne($dataInsert, $data2, $data3);
		if ($result) {
			$SESS = array(
				'uid' => $result,
				'nickname' => $dataInsert['nickname'],
				'username' => $dataInsert ['username'],
				'address_id' => array($data3['address_id']),
				'role' => parent::ROLE_DILIVERIER,
				'status' => parent::STATUS_DELETE,
				'updated_at' => $time,
				parent::__ID => REST_Diliveriers_Controller::__ID_PARAM
			);
			$this->session->set_userdata($SESS);
			session_write_close();
			$data ['id'] = $result;
			$data ['nickname'] = $SESS['nickname'];
			$data ['username'] = $SESS['username'];
			$data ['user_head_img'] = base_url('data/share/upload/head_img/14405938061421.jpg');
			$data ['role'] = $SESS['role'];
			$data ['status'] = parent::STATUS_DELETE;
			$data [config_item('user.tokenParam')] = $token . '.' . $SESS ['updated_at'];
			$this->renderJson(11100, array('data' => $data));
		} else {
			$this->renderJson(11201);
		}
	}

	public function upload() {
		if ($this->isGuest()) {
			$this->renderJson(10002);
		}

		if ($this->identity['status'] > parent::STATUS_DELETE) {
			$this->renderJson(10056);
		}

		$images = $this->Agent->unlinkImages($this->identity['uid']);
		if(!empty($images)) {
			foreach ($images as $v) {
				if(!empty($v['img']) && file_exists($v['img'])) {
					unlink($v['img']);
				}
			}
		}
		$config ['upload_path'] = config_item('upload_path') . '/identification/' . date('Ymd');
		if (!is_dir($config ['upload_path'])) {
			mkdir($config ['upload_path'], 0755, true);
		}
		$config ['file_name'] = time() . rand(1000, 9999);
		$config ['allowed_types'] = 'jpg|bmp|jpeg|gif|png';

		$this->load->library('upload', $config);
		$data = array();
		foreach ($_FILES as $k => $v) {
			if (!in_array($k, array('autodyne', 'front', 'reverse'))) {
				continue;
			}
			if (!$this->upload->do_upload($k)) {
				$this->renderJson(11200, array('msg' => $this->upload->display_errors()));
			} else {
				$this->load->model('backend/Album_model', 'Album');
				$dataInsert = array(
					'source_id' => $this->identity['uid'],
					'img' => $config ['upload_path'] . '/' . $this->upload->data('file_name'),
					'type' => '1', //展示类型
					'from' => '4', //来源身份证
					'created_at' => time()
				);
				$id = $this->Album->insert($dataInsert);
				$data[$k . '_id'] = $id;
			}
		}
		if (!empty($data)) {
			$data['updated_at'] = time();
			$result = $this->Agent->updateOne($data, $this->identity['uid']);
			if ($result) {
				$this->renderJson(11100);
			}
		}
		$this->renderJson(11200);
	}

	/**
	 * 登陆
	 */
	public function login() {
		$this->form_validation->validateData(array(
			'username',
			'password'
				), array(
			array(
				'username',
				'mobile'
			),
			array(
				'password',
				'required|min_length[6]|validate_password[agent.username]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		if (!empty($this->form_validation->user->access_token)) {
			session_id($this->form_validation->user->access_token);
		}
		$this->load->library('session');
		$this->session->sess_regenerate(TRUE);
		$token = session_id();
		$dataUpdate = array(
			'access_token' => $token,
			'updated_at' => time()
		);
		$uid = $this->form_validation->user->id;
		$result = $this->Agent->update($dataUpdate, array(
			'id' => $uid
		));
		if ($result) {
			$addressIds = $this->Agent->getAddressId($uid);
			$SESS = array(
				'uid' => $uid,
				'nickname' => $this->form_validation->user->nickname,
				'username' => $this->form_validation->username,
				'address_id' => $addressIds,
				'role' => $this->form_validation->user->role,
				'updated_at' => $dataUpdate ['updated_at'],
				'status' => $this->form_validation->user->status,
				parent::__ID => parent::__ID_PARAM
			);
			$this->session->set_userdata($SESS);
			session_write_close();
			$data ['id'] = $SESS['uid'];
			$data ['nickname'] = $SESS['nickname'];
			$data ['username'] = $SESS['username'];
			$data ['user_head_img'] = base_url('data/share/upload/head_img/14405938061421.jpg');
			$data ['role'] = $SESS['role'];
			$data ['status'] = $SESS['status'];
			$data [config_item('user.tokenParam')] = $token . '.' . $SESS ['updated_at'];
			$this->renderJson(11100, array('data'=>$data));
		} else {
			$this->renderJson(11201);
		}
	}

	/**
	 * 忘记密码
	 */
	public function forget() {
		$this->form_validation->validateData(array(
			'username',
			'code',
			'newPassword'
				), array(
			array(
				'username',
				'mobile'
			),
			array(
				'code',
				'required|integer'
			),
			array(
				'newPassword',
				'required|min_length[6]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->library('vendor_interface');
		$ret = $this->vendor_interface->verify($this->form_validation->username, $this->form_validation->code);
		if (!$ret) {
			$this->renderJson(10101);
		}

		$data = array(
			'password_hash' =>
			md5(md5($this->form_validation->newPassword))
		);
		$result = $this->Agent->update($data, array('username' => $this->form_validation->username));
		$this->renderJson(11100);
	}

	/**
	 * 重置密码
	 */
	public function reset() {
		if ($this->isGuest()) {
			$this->renderJson(10002);
		}
		$this->form_validation->validateData(array(
			'password',
			'newPassword'
				), array(
			array(
				'password',
				'required|min_length[6]|validate_old_password[agent.username]'
			),
			array(
				'newPassword',
				'required|min_length[6]|differs[password]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$data = array(
			'password_hash' => md5(md5($this->form_validation->newPassword))
		);
		$result = $this->Agent->update($data, array(
			'id' => $this->identity ['uid']
				));
		if ($result) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}
	/**
	 * android版本
	 */
	public function version() {
		$this->load->config('system_info');
		$data = array('data' => array(
				'versioncode' => intval(config_item('cfg_d_android_version')),
				'downloadUrl' => config_item('cfg_d_android_download'),
				'desc' => config_item('cfg_d_android_update_desc')
		));
		$this->renderJson(11100, $data);
	}
	

	
}
